﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Data;
using ClassLibrary.DAO;

public partial class Admin_ucControl_ucChipInsUp : System.Web.UI.UserControl
{
    TblChipDAO chipDAO = new TblChipDAO(Global.CONNECTION_STRING);
    TblTypeDAO typeDAO = new TblTypeDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            loadType();
           // loadDetail();
            string id = Request.QueryString["id"];

            if (id == "" || id == null)
            {
                loadType();
            }
            else
            {
                LoadDetailChip();
            }
        }
    }

    private void LoadDetailChip()
    {
        string id = Request.QueryString["id"];
        TblChip chip = chipDAO.GetChipByID(Convert.ToInt32(id));
        txtChipName.Text = chip.ChipName;
        txtPrice.Text = Convert.ToDecimal(chip.Price).ToString();
        description.Content = chip.ItemDescription;
        drIDType.SelectedValue = Convert.ToInt32(chip.IDType).ToString();
    }

    private void loadType()
    {
        try
        {
            drIDType.DataSource = typeDAO.GetAll();
            drIDType.DataTextField = "TypeName";
            drIDType.DataValueField = "IDType";
            drIDType.DataBind();

        }
        catch
        {
             
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblChip chip = new TblChip();
            chip.IDType = Global.ConvertToInt(drIDType.SelectedValue);
            chip.ItemDescription = Server.HtmlDecode(description.Content);
            chip.ChipName = txtChipName.Text;
            chip.Price = Convert.ToDecimal(txtPrice.Text);

            if (id == null || id == "")
            {
                chip.IDChip = 0;
                chipDAO.InsUpdItem(chip, 1);
            }
            else
            {
                chip.IDChip = Global.ConvertToInt(id);
                chipDAO.InsUpdItem(chip, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
             ScriptManager.RegisterStartupScript(btnSave, btnSave.GetType(), "alert", "alert(' Insert Sucessful!');", true);
            Response.Redirect("~/Admin/Default.aspx");
        }
    }
}
